package walasp.proto;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

/**
 * 
 * Activity for viewing a Task.
 *
 */
public class ViewActivity extends Activity {
	TextView Conty;
	Task task;
	int tid;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewtask);
		tid = getIntent().getIntExtra("TID", 0);
		if (tid == 0) {
			Log.e("walasp.proto.ViewActivity",
					"missing TID from intent, exiting");
			finish();
		}
		TaskHelper helper = new TaskHelper(getBaseContext());
		task = helper.getTask(tid);
		helper.close();
		if (task == null) {
			Log.e("walasp.proto.ViewActivity",
					"unable to get task from DB, exiting");
			finish();
		}
		drawValues();
	
	}
	
	@Override
	public void onResume() {
		TaskHelper helper = new TaskHelper(getBaseContext());
		task = helper.getTask(tid);
		helper.close();
		if (task == null) {
			Log.e("walasp.proto.ViewActivity",
					"unable to get task from DB, exiting");
			finish();
		}
		drawValues();

		super.onResume();
	}

	/**
	 * Sets All values on the screen to the most up to date. 
	 */
	private void drawValues(){
		TextView text = (TextView) findViewById(R.id.viewName);
		if (text == null) {
			Log.e("walasp.proto.ViewActivity", "Textfield is null");
		}
		text.setText("stuff");
		((TextView) findViewById(R.id.viewName)).setText(task.getTaskName());
		if (task.getStatus().equals("ACTIVE")) {
			if (task.getDueDate().getTime() == 0) {
				((TextView) findViewById(R.id.viewDate)).setText(" ");
			} else {
				((TextView) findViewById(R.id.viewDate)).setText(task
						.getDueDate().toLocaleString());
			}
		} else {
			((TextView) findViewById(R.id.viewDate)).setText(task.getStatus());
		}
		String contact = task.getContacts();
		if (contact == null) {
			((TextView) findViewById(R.id.viewContacts)).setText("");
		} else {

			Conty = (TextView) findViewById(R.id.viewContacts);
			Conty.setText(getContactName(contact));
			Conty.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {

					Intent intent = new Intent(Intent.ACTION_VIEW);
					Uri uri = Uri.parse(task.getContacts());
					intent.setData(uri);
					startActivity(intent);

				}
			});
		}
		((TextView) findViewById(R.id.viewDesc)).setText(task.getTaskContent());
	}
	
	/**
	 * Retrieves the name of the contact from the database.
	 * @param contact
	 * 			the Contact information string
	 * @return the Contact name
	 */
	private String getContactName(String contact) {
		Uri con = Uri.parse(contact);
		Cursor res = managedQuery(con, null, null, null, null);
		// Cursor res = m
		res.moveToFirst();
		String name;
		try {
			name = res.getString(res
					.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
		} catch (Exception e) {
			name = " ";
		}
		return name;

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.viewmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.viewMenuEdit:
			Intent intent = new Intent(getBaseContext(), EditActivity.class);
			intent.putExtra("mode", 1);
			intent.putExtra("TID", task.getTID());
			startActivityForResult(intent, 0);
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

}
